package net.ib.mtalk.database.helper;

import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.util.Log;
import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.FileKey;
import net.ib.mntalk.protocol.Common.SuggestionId;
import net.ib.mntalk.protocol.Common.SuggestionInfo;
import net.ib.mntalk.protocol.Common.SuggestionReason;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Session.AddBuddyReq;
import net.ib.mntalk.protocol.Session.GetSuggestionsRes;
import net.ib.mntalk.protocol.Session.SetSuggestionsReq;
import net.ib.mntalk.protocol.Session.SetSuggestionsRes;
import net.ib.mtalk.database.DatabaseContract.BuddyTable;
import net.ib.mtalk.database.DatabaseContract.ContactTable;
import net.ib.mtalk.database.DatabaseContract.SuggestionTable;
import net.ib.mtalk.network.task.ProfileDownloadTask;
import net.ib.mtalk.util.GpbSupport;

public class SuggestionDatabaseHelper {
	
	/**
	 * insertSuggestions
	 * @param mUiHandler 
	 * @param Context - context
	 * @param GetSuggestionsRes - getSuggestionsRes
	 */
	public static void insertSuggestions(Context context, Handler uiHandler, GetSuggestionsRes getSuggestionsRes) {
		ContentValues cv  = new ContentValues();
		
		List<SuggestionInfo> list = getSuggestionsRes.getSuggestionInfoList();
		if(list != null){
			for(SuggestionInfo info : list){
				cv.clear();
				FileKey fileKey = null;
				SuggestionId suggestionId = info.getSuggestionId();
				UUIDMsg uuid = suggestionId.getObjectUuid();
				cv.put(SuggestionTable.KEY_SUGGESTION_UUID_MSB, GpbSupport.msbOf(uuid));
				cv.put(SuggestionTable.KEY_SUGGESTION_UUID_LSB, GpbSupport.lsbOf(uuid));
				cv.put(SuggestionTable.KEY_SCORE, suggestionId.getScore());
				cv.put(SuggestionTable.KEY_STATUS_TYPE, info.getStatusType());
				List<SuggestionReason> reasons = info.getReasonList();
				if(reasons != null){
					for(SuggestionReason reason : reasons){
						if(reason.getType() == Common.SUGGESTION_REASON_MUTUAL){
//							cv.put(SuggestionTable.KEY_MUTUAL_OPTIONS, reason.getMutualOptions().toByteArray());
						} else{
							fileKey = reason.getKnowsOptions().getKnowsProfile().getProfileImage();
							cv.put(SuggestionTable.KEY_KNOWS_OPTIONS, reason.getKnowsOptions().toByteArray());
							cv.put(SuggestionTable.KEY_DISPLAYNAME, reason.getKnowsOptions().getKnowsProfile().getName().getDisplayName());
							ContentResolver cr = context.getContentResolver();
							cr.insert(SuggestionTable.CONTENT_URI, cv);
						}
					}
				}
				if(fileKey == null){
					Log.i("ZOO", "파일키 없음");
					continue;
				}
				ProfileDownloadTask task = new ProfileDownloadTask(context, uiHandler, uuid, fileKey, null, -1);
				task.execute();
			}
		}
	}

	/**
	 * blockSuggestion
	 * @param Context - context
	 * @param SetSuggestionsReq - setSuggestionsReq
	 */
	public static void updateSuggestionStatus(Context context, SetSuggestionsReq setSuggestionsReq) {
		ContentValues cv = new ContentValues();
		cv.put(SuggestionTable.KEY_SUGGESTION_UUID_MSB, GpbSupport.msbOf(setSuggestionsReq.getTarget(0).getObjectUuid()));
		cv.put(SuggestionTable.KEY_SUGGESTION_UUID_LSB, GpbSupport.lsbOf(setSuggestionsReq.getTarget(0).getObjectUuid()));
		cv.put(SuggestionTable.KEY_STATUS_TYPE, setSuggestionsReq.getStatusType());
		String where = SuggestionTable.KEY_SUGGESTION_UUID_MSB + "=? AND " + SuggestionTable.KEY_SUGGESTION_UUID_LSB + "=?";
		String[] selectionArgs = {String.valueOf(GpbSupport.msbOf(setSuggestionsReq.getTarget(0).getObjectUuid())),
								  String.valueOf(GpbSupport.lsbOf(setSuggestionsReq.getTarget(0).getObjectUuid()))};
		context.getContentResolver().update(SuggestionTable.CONTENT_URI, cv, where, selectionArgs);
	}

	/**
	 * deleteSuggestion
	 * @param Context - context
	 * @param AddBuddyReq - mAddBuddyReq
	 */
	public static void deleteSuggestion(Context context, AddBuddyReq mAddBuddyReq) {
		String where = SuggestionTable.KEY_SUGGESTION_UUID_MSB + "=? AND " + SuggestionTable.KEY_SUGGESTION_UUID_LSB + "=?";
		String[] selectionArgs = {String.valueOf(GpbSupport.msbOf(mAddBuddyReq.getBuddy().getUserUuid())), String.valueOf(GpbSupport.lsbOf(mAddBuddyReq.getBuddy().getUserUuid()))};
		context.getContentResolver().delete(SuggestionTable.CONTENT_URI, where, selectionArgs);
		
	}

	/**
	 * selectDeleteSuggestion
	 * @param Context - context
	 */
	public static Cursor selectDeleteSuggestion(Context context) {
		return context.getContentResolver().query(
				SuggestionTable.CONTENT_URI,
				null, 
				SuggestionTable.KEY_STATUS_TYPE + "=" + Common.SUGGESTION_STATUS_DELETE, 
				null, SuggestionTable.KEY_DISPLAYNAME);
	}
}
